import re

primitive_type_wo_tuple_list = {'i8', 'i16', 'i32', 'i64', 'i128', 'u8', 'u16', 'u32', 'u64', 'u128',
                  #'isize', 'usize',
                  'f32', 'f64', 'bool', 'char'}

as_repeat_pattern = r'(\w+)\s+as\s+(\w+)\s+/\*as\s+(\w+)\*/|(\w+)\s+as\s+(\w+)\s+/\*(\w+)\s+as\s+'

null_pointer_count = 0
dup_as_count = 0
some_func_count = 0

def get_type_trans_summary():
    return {
        "空指针优化个数": null_pointer_count,
        # "重复定义转换优化次数": dup_as_count,
        # "Some+函数优化次数": some_func_count
      }

def replace_non_primitive_null(content):

    global null_pointer_count

    # 定义模式，匹配 0 as *mut SomeType 表达式
    pattern = re.compile(r'(s+|\()0 as \*mut (\w+)')

    iter = re.finditer(pattern, content)
    for match in iter:
        if not match.group(2) in primitive_type_wo_tuple_list:
            null_pointer_count += 1

    content = pattern.sub(lambda match:
        match.group(1) + 'core::ptr::null_mut()' if not match.group(2) in primitive_type_wo_tuple_list else
        match.group(1) + '0 as *mut ' + match.group(2), content)

    pattern = re.compile(r'(s+|\()0 as (\w+)')

    iter = re.finditer(pattern, content)
    for match in iter:
        if not match.group(2) in primitive_type_wo_tuple_list:
            null_pointer_count += 1

    content = pattern.sub(lambda match:
                          match.group(1) + 'core::ptr::null_mut()' if not match.group(2) in primitive_type_wo_tuple_list else
                          match.group(1) + '0 as ' + match.group(2), content)

    null_pointer_count += len(re.findall(r' 0 as \*mut \(\)', content))
    null_pointer_count += len(re.findall(r' 0 as \(\)', content))

    content = re.sub(r' 0 as \*mut \(\)', r' core::ptr::null_mut()', content)
    content = re.sub(r' 0 as \(\)', r' core::ptr::null()', content)
    #content = re.sub(r'\(0 as \*mut \(\)', r'\(core::ptr::null_mut()', content)
    #content = re.sub(r'\(0 as \(\)', r'\(core::ptr::null()', content)

    return content

def manage_type_trans(content):

    global null_pointer_count
    global dup_as_count
    global some_func_count

    # 模式匹配所有数据类型转换最终结果为基本数据类型的代码段，将其转换操作全部去除，因为在Rust中基本数据类型是不需要显式定义的。
    # as i8和as *mut i8分开处理，0 as *mut libc::c_void 和 0 as *mut SomeType 特殊处理
    # TODO: 考虑到如 malloc(::core::mem::size_of::<QueueEntry>() as u64) 的情况(malloc是c函数)，后续该功能可能通过选项启用
    # for item in primitive_type_list:
    #     content = content.replace(f' as {item}', '').replace(f' as *mut {item}', '')

    content = replace_non_primitive_null(content)

    null_pointer_count += content.count(' 0 as ()')
    null_pointer_count += content.count(' 0 as mut* ()')
    content = content.replace(' 0 as ()', ' core::ptr::null()').replace(' 0 as mut* ()', ' core::ptr::null_mut()')

    pattern = r'\s+(\d+) as \w+'
    # 替换匹配的模式为仅[数字]
    content = re.sub(pattern, r' \1', content)

    # 例如 as i8 as i8，通过模式匹配定位重复的as操作去除。
    pattern = re.compile(r'as \*mut (\w+)(\s+)as \*mut \1')
    dup_as_count += len(re.findall(pattern, content))
    content = pattern.sub(r'as *mut \1', content)

    # 将Some(function_name as fn(type))简化为Some(function_name)
    # 引起所有cargo test失败，故丢弃。
#     pattern = re.compile(r'Some\((\w+)\sas\s(unsafe\s)?(extern\s\"C\"\s)?fn\([^)]*?\)\)')
#     some_func_count += len(re.findall(pattern, content))
#     content = pattern.sub(r'Some(\1)', content)

    return content
